package org.example.servlet;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.example.entity.Order;
import org.example.response.ServerResponse;
import org.example.service.IOrderService;
import org.example.util.IocUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "OrderListServlet",urlPatterns = "/order/list")
public class OrderListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.校验----参数校验
        int page=1,limit=3;
        if(req.getParameter("page")!=null){
            page = Integer.parseInt(req.getParameter("page"));
        }
        if(req.getParameter("limit")!=null){
            limit = Integer.parseInt(req.getParameter("limit"));
        }
        //2.调用service
        IOrderService iOrderService = IocUtil.getBean(IOrderService.class);
        PageInfo<Order> orderPageInfo = iOrderService.selectByPage(page, limit);
        System.out.println(orderPageInfo.getList());
        //3.返回响应-------list------->json=====>fastJson

//        ServerResponse response = new ServerResponse();
//        response.setCode(0);
//        response.setMsg("success");
//        response.setCount(orderPageInfo.getTotal());
//        response.setData(orderPageInfo.getList());
//        String resultJson = JSON.toJSONString(response);

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print(ServerResponse.<Order>success(orderPageInfo.getList(),orderPageInfo.getTotal()));
    }
}
